package cn.exlu.wxcall;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import cn.exlu.wxcall.adapter.UserInfo;
import cn.exlu.wxcall.adapter.UserInfoAdapter;
import cn.exlu.wxcall.util.StringUtil;

public class WXCallList_Activity extends Activity {
	private Context context;
	ListView lv;
	List<UserInfo> list = new ArrayList<UserInfo>();
	

	@Override
	protected void onStart() {
		super.onStart();
		// 获取已保存数据
		SharedPreferences sharedPref = context.getSharedPreferences(
		        getString(R.string.data_userinfos), Context.MODE_PRIVATE);
		
		Map<String, ?> userinfosMap = sharedPref.getAll();
		
		list.clear();
		if(userinfosMap !=null){
			for(String key :userinfosMap.keySet()){
				String val = (String)userinfosMap.get(key);
				if(StringUtil.isNullOrEmpty(val)){
					continue;
				}
				
				UserInfo userinfo = new UserInfo(val);
				
				if(StringUtil.isNullOrEmpty(userinfo.getUserName())){
					continue;
				}
				if(StringUtil.isNullOrEmpty(userinfo.getUserPhone())){
					continue;
				}
				
				list.add(userinfo);
			}
		}
		UserInfoAdapter adapter = (UserInfoAdapter)lv.getAdapter();
		adapter.notifyDataSetChanged();

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wxcall_list_);
		context = this;
		
//		// 获取已保存数据
//		SharedPreferences sharedPref = context.getSharedPreferences(
//		        getString(R.string.data_userinfos), Context.MODE_PRIVATE);
//		
//		Map<String, ?> userinfosMap = sharedPref.getAll();
//		list = new ArrayList<UserInfo>();
//		
//		if(userinfosMap !=null){
//			for(String key :userinfosMap.keySet()){
//				String val = (String)userinfosMap.get(key);
//				if(StringUtil.isNullOrEmpty(val)){
//					continue;
//				}
//				
//				UserInfo userinfo = new UserInfo(val);
//				
//				if(StringUtil.isNullOrEmpty(userinfo.getUserName())){
//					continue;
//				}
//				if(StringUtil.isNullOrEmpty(userinfo.getUserPhone())){
//					continue;
//				}
//				
//				list.add(userinfo);
//			}
//		}
		
		lv =(ListView)this.findViewById(R.id.listViewUserInfo);
		
		lv.setAdapter(new UserInfoAdapter(this, R.layout.wxcall_list_item, list));
		lv.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				UserInfoAdapter adapter = (UserInfoAdapter)lv.getAdapter();
				UserInfo user = adapter.getItem(position);

				Intent intent = new Intent(Intent.ACTION_CALL);
				Uri data = Uri.parse("tel:" + user.getUserPhone());
				intent.setData(data);
				startActivity(intent);
				
				return true;
			}
			
		});
		
//		lv.setOnItemClickListener(new OnItemClickListener(){
//
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//					int position, long id) {
//				UserInfoAdapter adapter = (UserInfoAdapter)lv.getAdapter();
//				UserInfo user = adapter.getItem(position);
//
//				Intent intent = new Intent(Intent.ACTION_CALL);
//				Uri data = Uri.parse("tel:" + user.getUserPhone());
//				intent.setData(data);
//				startActivity(intent);
//			}
//			
//		});
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.wxcall_list_, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_add) {
			Intent intent = new Intent(WXCallList_Activity.this, MainActivity.class);
			startActivity(intent);
			
			
			return true;
		}else if(id == R.id.action_del){
			Intent intent = new Intent(WXCallList_Activity.this, WXCallDel_Activity.class);
			startActivity(intent);
			
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
